#ifndef MCACHE_KETAMA_H
#define MCACHE_KETAMA_H


namespace mcache {

#include <fnv/fnv.h>
#include <pcre++.h>

	using namespace pcrepp;

	class Ketama {

	public:
		Ketama(const string& servers) {
			vector<string> sl = getServerList(server.c_str());
			for(int i=0; i<sl.size(); i++) {
				float pct = 1.0 / (float)sl.size();
				int hpct = floorf(pct*100.0);
				int ks = floorf(pct*40.0*(float)sl.size());
				for(int k=0; k<ks; k++) {
					char ss[64];
					snprintf(ss, sizeof(ss), "%s-%d", sl[i].c_str(), k);
					Fnv32_t h = fnv_32a_buf(ss, strlen(ss), FNV1_32A_INIT);
					m_mcs.push_back(MCS(h, sl[i]));
				}
			}
		}

		string getServer(void* key, size_t size) {
			Fnv32_t h = fnv_32a_buf(key, size, FNV1_32A_INIT);
		}

	private:
		static vector<string> getServerList(const char* servers) {
			Pcre re("((\\d+\\.){3}\\d+:\\d+)");
			vector<string> ret;
			int offset = 0;
			while (re.search(servers, offset)) {
				ret.push_back(re.get_match(0));
				offset = re.get_match_end();
			}
			return ret;
		}

		struct MCS {
			unsigned int point;
			string ip;
			MSC(unsigned int point_, const string& ip_) : point(point_), ip(ip_) {}
			MSC(const MCS& m) : point(m.point), ip(m.ip) {}
		};

		int m_points;
		vector<MCS> m_mcs;
	};

}

#endif
